import 'package:flutter/material.dart';
import 'package:lite_message/style/lm_font_style.dart';
import 'package:lite_message/style/lm_icons.dart';
import 'package:lite_message/util/app_util.dart';
import 'package:lite_message/util/dio_util.dart';
import 'package:lite_message/widgets/custom_bg.dart';
import 'package:lite_message/widgets/lm_button.dart';
import 'package:lite_message/widgets/lm_text_field.dart';
import 'package:lite_message/widgets/logo.dart';

import 'forget_psw.dart';
import 'lang_btn.dart';
import 'regist_page.dart';

//登录界面
class LoginPage extends StatefulWidget {
  const LoginPage({Key key}) : super(key: key);

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  Map _data;

  @override
  void initState() {
    super.initState();

    _data = Map<String, dynamic>();
    _data['username'] = "";
    _data['password'] = "";
  }

  @override
  void dispose() {
    super.dispose();
  }

  ///登录方法
  _login() async {
    await DioUtil.login(context, _data);
  }

  ///注册方法
  _regist() async {
    await LM.jump(context, const RegistPage());
  }

  ///忘记密码
  _forgetPsw() async {
    await LM.jump(context, const ForgetPsw());
  }

  @override
  Widget build(BuildContext context) {
    return CustomBg(
      showBack: false,
      brightness: Brightness.light,
      appBarColor: Colors.blue,
      action: const LangBtn(),
      body: ListView(
        padding: EdgeInsets.symmetric(horizontal: 90.px),
        physics: LM.LmPhysics,
        children: [
          const Logo(),
          SizedBox(height: 60.px),
          Text(
            'LITE',
            textAlign: TextAlign.center,
            style: LMFontStyle.title1(context),
          ),
          Text(
            'MESSAGE',
            textAlign: TextAlign.center,
            style: LMFontStyle.title3(context),
          ),
          SizedBox(height: 80.px),
          LMTextField(hintText: '账号', onChanged: (v) => _data['username'] = v),
          SizedBox(height: 60.px),
          LMTextField(
            hintText: '密码',
            obscureText: true,
            icon: LmIcons.lock,
            onChanged: (v) => _data['password'] = v,
          ),
          SizedBox(height: 40.px),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              LmButton(
                color: Theme.of(context).canvasColor,
                text: '忘记密码',
                height: 100.px,
                style: TextStyle(
                  fontSize: 40.sp,
                  color: Theme.of(context).accentColor,
                  fontWeight: FontWeight.bold,
                ),
                onPressed: () async => await _forgetPsw(),
              ),
              LmButton(
                color: Theme.of(context).canvasColor,
                text: '注册',
                height: 100.px,
                style: TextStyle(
                  fontSize: 40.sp,
                  color: Theme.of(context).accentColor,
                  fontWeight: FontWeight.bold,
                ),
                onPressed: () async => await _regist(),
              ),
            ],
          ),
          SizedBox(height: 40.px),
          LmButton(
            onPressed: () async => await _login(),
            text: '登录',
          ),
        ],
      ),
    );
  }
}
